<?php
global $_GPC, $_W;
$weid = $this->_weid;
$action = 'coreuser';

$storeid = intval($_GPC['storeid']);
$this->checkStore($storeid);
$title = $this->actions_titles[$action];
$returnid = $this->checkPermission($storeid);
$cur_store = $this->getStoreById($storeid);
$GLOBALS['frames'] = $this->getNaveMenu($storeid,$action);

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $strwhere = " AND m.storeid={$storeid} ";
    if (!empty($_GPC['keyword'])) {
        $strwhere .= " AND (m.name LIKE '%{$_GPC['keyword']}%') ";
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']) + 86399;
        $strwhere .= " AND log.dateline >= {$starttime} AND log.dateline <= {$endtime} ";
    }

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }


    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $list = pdo_fetchall("SELECT log.*, m.name, s.company FROM " . tablename($this->table_material_log) . " log left join " . tablename($this->table_material) . " m on m.id=log.materialid left join " . tablename($this->table_supplier) . " s on s.id=log.supplierid WHERE log.weid = :weid $strwhere ORDER BY m.id DESC LIMIT
" . ($pindex - 1) * $psize . ',' . $psize, array(':weid' => $this->_weid));

    if (!empty($list)) {
        $totalall = pdo_fetch("SELECT COUNT(1) as total, SUM(log.price) as money, SUM(log.count) as count, SUM(m.count) as num FROM " . tablename($this->table_material_log) . " log left join " . tablename($this->table_material) . " m on m.id=log.materialid WHERE log.weid = :weid $strwhere", array(':weid' => $this->_weid));
        $total = $totalall['total'];
        $money = $totalall['money'];
        $count = $totalall['count'];
        $num = $totalall['num'];
        $pager = pagination($total, $pindex, $psize);
    }
} else if ($operation == 'post') {
    $id = intval($_GPC['id']);
    $material = pdo_fetchall("SELECT * FROM " . tablename($this->table_material) . " WHERE weid = :weid AND storeid=:storeid ORDER BY id DESC", array(':weid' => $this->_weid, ':storeid' => $storeid));
    $log = pdo_fetch("SELECT * FROM " . tablename($this->table_material_log) . " WHERE weid = :weid AND id=:id AND storeid=:storeid ORDER BY id DESC", array(':weid' => $this->_weid, ':id' => $id, ':storeid' => $storeid));
    if (checksubmit('submit')) {
        $materialid = intval($_GPC['materialid']);
        $count = intval($_GPC['count']);
        $material = pdo_fetch("SELECT * FROM " . tablename($this->table_material) . " WHERE weid = :weid AND id=:id", array(':weid' => $this->_weid, ':id' => $materialid));
        $data = array(
            'weid' => $this->_weid,
            'storeid' => intval($_GPC['storeid']),
            'supplierid' => $material['supplierid'],
            'materialid' => $materialid,
            'count' => $count,
            'price' => $material['price'] * $count,
            'dateline' => TIMESTAMP,
        );

        if (empty($id)) {
            pdo_query("UPDATE " . tablename($this->table_material) . " SET count=count+".$count." WHERE storeid=:storeid AND id=:id", array(':storeid' => $storeid, 'id' => $materialid));
            pdo_insert($this->table_material_log, $data);
        } else {
            if ($count != $log['count']) {
                $ccount = $count - $log['count'];
                $newprice = $ccount * $material['price'];
                $data['price'] = $log['price'] + $newprice;
                pdo_query("UPDATE " . tablename($this->table_material) . " SET count=count+".$ccount." WHERE storeid=:storeid AND id=:id", array(':storeid' => $storeid, 'id' => $materialid));
            }
            pdo_update($this->table_material_log, $data, array('id' => $id));
        }
        message('操作成功！', $this->createWebUrl('material_log', array('op' => 'display', 'storeid' => $storeid), true));
    }
} else if ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT id FROM " . tablename($this->table_material_log) . " WHERE id = :id AND weid=:weid ", array(':id' => $id, ':weid' => $_W['uniacid']));
    if (empty($item)) {
        message('抱歉，不存在或是已经被删除！', $this->createWebUrl('material_log', array('op' => 'display')), 'error');
    }
    pdo_query("UPDATE " . tablename($this->table_material) . " SET count=count-".$item['count']." WHERE storeid=:storeid AND id=:id", array(':storeid' => $storeid, 'id' => $id));
    pdo_delete($this->table_material_log, array('id' => $id, 'weid' => $_W['uniacid']));
    message('删除成功！', $this->createWebUrl('material_log', array('op' => 'display', 'storeid' => $storeid)), 'success');
}
include $this->template('web/material_log');